
{template '../web_common/header'}

<!-- 中间内容 -->
<div class="mainContent clearfix">
    <div class="center-con clearfix">
        <h2 class="main-ttl">添加问题</h2>
        <div class="main-ctn clearfix" style="padding-bottom: 80px;">
            <div class="item clearfix">
                <div class="l text-right">
                    <span class="c-red">*</span><span>问题：</span>
                </div>
                <div class="r">
                    <input type="text" style="width: 1000px" class="input-text" name="question" value="{$question_item['question']}">
                </div>
            </div>
            <div class="item clearfix">
                <div class="l text-right">
                    <span>设为常见：</span>
                </div>
                <div class="r">
                    <div><input type="checkbox" name="is_common" {if $question_item['is_common'] == 1}checked="checked"{/if}></div>
                </div>
            </div>
            {if $question_item['similar_question']}
            {loop $question_item['similar_question'] $index $item}
            <div class="item clearfix">
                <div class="l text-right">
                    <span>相似问法：</span>
                </div>
                <div class="r" >
                    <input class="input-text" name="similar_question" type="text" value="{$item}" style="width: 926px">
                    <button type="button" class="Btn del_q ml10" onclick="del($(this))">删除</button>
                </div>
            </div>
            {/loop}
            {/if}
            <div class="item clearfix">
                <div class="r" style="margin-left: 80px;">
                    <p style="color: #ff0000;font-size: 13px">注意: 添加相似问法是为了在客服端更好地搜到这个问题, 并不是添加新的问题哦~</p>
                    <button type="button" class="Btn" id="add_q">添加相似问法</button>
                </div>
            </div>
            <div class="item clearfix s_fl">
                <div class="l text-right">
                    <span class="c-red">*</span><span>选择分类：</span>
                </div>
                <div class="r">
                    <select class="form-control cat_id" name="cat_id">
                        <option value="0">请选择</option>
                    {if $category_list}
                    {loop $category_list $index $item}
                        <option style="font-weight:bold" value="{$item['cat_id']}" {if $item['cat_id'] == $question_item['cat_id']}selected="selected"{/if} >{$item['cat_name']}</option>
                        {if $item['child_list']}
                        {loop $item['child_list'] $k $v}
                            <option value="{$v['cat_id']}" {if $v['cat_id'] == $question_item['cat_id']}selected="selected"{/if}>--{$v['cat_name']}</option>
                        {/loop}
                        {/if}
                    {/loop}
                    {/if}
                    </select>
                </div>
            </div>
            <div class="item clearfix">
                <div class="l text-right">
                    <span class="c-red">*</span><span>答案：</span>
                </div>
                <div class="r">
                    <div style="width: 1000px;">
                        <!-- 加载编辑器的容器 -->
                        <textarea id="editor_id" name="content">{$question_item['answer']}</textarea>
                    </div>
                </div>
            </div>
            <div class="item clearfix is_show">
                <div class="l text-right">
                    <span>标签：</span>
                </div>
                <div class="r">
                    <ul class="clearfix flList" style="width: 1000px">
                        {if $label_list}
                        {loop $label_list $index $item}
                            <li><input id="checkbox-{$item['label_id']}" name="label_ids" type="checkbox" value="{$item['label_id']}" {if $item['is_selected'] == 1}checked="checked"{/if} ><label >{$item['label_name']}</label></li>
                        {/loop}
                        {/if}
                    </ul>
                </div>
            </div>
            <div class="item clearfix mt20" style="position: absolute;left: 20px;bottom: 20px;">
                {if $_GPC['type'] == 'add'}
                <button type="submit" class="Btn operate_question" data-content="again">保存并继续添加</button>
                {/if}
                <button type="submit" class="Btn ml10 operate_question">保存</button>
                <button type="button" class="Btn ml10" id="return" onclick="window.location.href='{$back}'">返回</button>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript" src="{KB_WEB_BACK_JS}/ueditor/ueditor.config.js"></script>
<!-- 编辑器源码文件 -->
<script type="text/javascript" src="{KB_WEB_BACK_JS}/ueditor/ueditor.all.js"></script>

<script>
    var ue = UE.getEditor('editor_id', {
        toolbars: [[
            'source', //源代码
            '|',
            'undo', //撤销
            'redo', //重做
            '|',
            'bold', //加粗
            'indent', //首行缩进
            'italic', //斜体
            'underline', //下划线
            'strikethrough', //删除线
            'subscript', //下标
            'superscript', //上标
            '|',
            'fontfamily', //字体
            'fontsize', //字号
            '|',
            'justifyleft', //居左对齐
            'justifyright', //居右对齐
            'justifycenter', //居中对齐
            'justifyjustify', //两端对齐
            'insertorderedlist', //有序列表
            'insertunorderedlist', //无序列表
            '|',
            'formatmatch', //格式刷
            'pasteplain', //纯文本粘贴模式
            'selectall', //全选
            'horizontal', //分隔线
            'removeformat', //清除格式
            'link', //超链接
            'unlink', //取消链接
            '|',
            'inserttable', //插入表格
            'mergeright', //右合并单元格
            'mergedown', //下合并单元格
            'splittorows', //拆分成行
            'splittocols', //拆分成列
            'splittocells', //完全拆分单元格
            'deletecaption', //删除表格标题
            'inserttitle', //插入标题
            'mergecells', //合并多个单元格
            'deletetable', //删除表格
            'simpleupload', //单图上传
            'insertimage', //多图上传
            'edittable', //表格属性
            'edittd', //单元格属性
            'emotion', //表情
            'forecolor', //字体颜色
            'imagenone', //默认
            'imageleft', //左浮动
            'imageright', //右浮动
            'imagecenter', //居中
            'lineheight', //行间距
            'autotypeset', //自动排版
        ]],
        initialFrameHeight: 450,
        elementPathEnabled: false,//去掉元素路径
        wordCount: false,//关闭字数统计
        maximumWords: 50000,//最大字符数
        autoHeightEnabled: false,//是否自动长高
        enableContextMenu: false
    });
</script>

<script type="text/javascript">
    require(['jquery', 'layer', 'common'], function ($, layer) {

        $('#add_q').on('click',function(){
            var html='<div class="item clearfix">' +
                '<div class="l text-right">' +
                '<span>相似问法：</span>' +
                '</div>' +
                '<div class="r">' +
                '<input type="text" class="input-text" name="similar_question" style="width: 926px">' +
                '<button type="button" class="Btn del_q ml10" onclick="del($(this))">删除</button>' +
                '</div>' +
                '</div>';
            $(this).parents('.item').before(html);
        });

        $('.operate_question').on('click', function () {
            var continue_add = $(this).attr('data-content');
            var similar_question = [],
                label_ids = [],
                question = $('input[name=question]').val(),
                is_common = $('input[name=is_common]').is(':checked') === true ? 1 : 0,
                cat_id = $('.cat_id :selected').val(),
                answer = ue.getContent();

            $('input[name="label_ids"]').each(function (index, element) {
                if (element.checked) {
                    label_ids.push($(this).val());
                }
            });

            $('input[name=similar_question]').each(function (index, element) {
                if ($(element).val().length > 0) {
                    similar_question.push($(element).val());
                }
            });

            if (question.length === 0) {
                layer.msg('请填写问题');
                return false;
            }
            if (cat_id === '0') {
                layer.msg('请选择类别');
                return false;
            }
            if (answer.length === 0) {
                layer.msg('请填写答案');
                return false;
            }

            var data = {
                'question_id' : "{$question_item['question_id']}",
                'cat_id' : cat_id,
                'question' : question,
                'answer' : answer,
                'is_common' : is_common,
                'label_ids' : label_ids,
                'similar_question' : similar_question
            };

            $.post("{php echo $this->createWebUrl('knowledge', array('act' => 'operate'))}", data, function (data) {
                if(data.code === 200){
                    layer.msg(data.msg, {icon: 1,time:1000});
                    setTimeout(function () {
                        if (continue_add === 'again') {
                            window.location.href = "{php echo $this->createWebUrl('knowledge', array('act' => 'operate', 'type' => 'add'));}";
                        } else {
                            window.location.href = "{$back}";
                        }
                    }, 1500)
                } else {
                    layer.msg(data.msg, {icon: 0,time:1000});
                    return false;
                }
            });
        });

    });

    //删除添加相似问法
    function del(obj){
        obj.parent().parent().remove();
    }

</script>

{template '../web_common/footer'}